// src/utils/config.js
// 环境配置管理

// 获取当前运行环境
const getEnvironment = () => {
    // 检查是否在Capacitor环境中运行 - 使用多种方法检测
    const isCapacitor = (
        window.Capacitor !== undefined ||
        window.capacitor !== undefined ||
        document.querySelector('html').classList.contains('capacitor') ||
        navigator.userAgent.includes('Capacitor') ||
        window.location.protocol === 'capacitor:'
    );

    // 检查是否在移动端浏览器中运行
    const isMobile = /Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);

    // 检查是否在Android环境中
    const isAndroid = /Android/i.test(navigator.userAgent);

    console.log('环境检测信息:', {
        userAgent: navigator.userAgent,
        isCapacitor,
        isMobile,
        isAndroid,
        protocol: window.location.protocol,
        hostname: window.location.hostname
    });

    if (isCapacitor || (isAndroid && window.location.hostname === 'localhost')) {
        return 'capacitor';
    }

    if (isMobile) {
        return 'mobile';
    }

    // 检查是否在开发环境
    if (process.env.NODE_ENV === 'development') {
        return 'development';
    }

    return 'production';
};

// API配置
const apiConfig = {
    development: {
        baseURL: 'http://localhost:8080',
        timeout: 5000
    },
    mobile: {
        baseURL: 'http://10.0.2.2:8080', // Android模拟器访问主机
        timeout: 10000
    },
    capacitor: {
        baseURL: 'http://10.0.2.2:8080', // Android模拟器访问主机
        timeout: 10000
    },
    production: {
        baseURL: 'https://your-production-api.com', // 生产环境API地址
        timeout: 10000
    }
};

// 获取当前环境的API配置
export const getApiConfig = () => {
    const env = getEnvironment();
    console.log('当前环境:', env);
    const config = apiConfig[env] || apiConfig.development;
    console.log('使用的API配置:', config);
    return config;
};

// 获取基础URL
export const getBaseURL = () => {
    const baseURL = getApiConfig().baseURL;
    console.log('API基础URL:', baseURL);

    return baseURL;
};

// 获取超时时间
export const getTimeout = () => {
    return getApiConfig().timeout;
};

// 导出环境信息
export const environment = getEnvironment();

// 调试信息
console.log('=== 环境配置初始化 ===');
console.log('当前运行环境:', environment);
console.log('API配置:', getApiConfig());
console.log('基础URL:', getBaseURL());
console.log('====================='); 